﻿using System;
using System.Collections.Generic;
using System.Text;
using RaisingStudio.Data.Common.Managers;
using RaisingStudio.Data.Common.Factories;

namespace RaisingStudio.Data.Common.Builders
{
    /// <summary>
    /// dataset Command builder.
    /// </summary>
    public class DataSetCommandBuilder
    {
        #region Common property 
        #region
        #endregion
        protected System.Data.IDbConnection connection;
        /// <summary>
        /// database connection.
        /// </summary>
        public System.Data.IDbConnection Connection
        {
            get
            {
                return this.connection;
            }
        }

        private DatabaseType _databaseType;
        /// <summary>
        /// Database type.
        /// </summary>
        public DatabaseType DatabaseType
        {
            get
            {
                return this._databaseType;
            }
            set
            {
                this._databaseType = value;
            }
        }

        private string _providerName;
        /// <summary>
        /// The name of provider.
        /// </summary>
        public string ProviderName
        {
            get
            {
                return this._providerName;
            }
            set
            {
                this._providerName = value;
            }
        }

        protected DataSetCommonCommandBuilder commonCommandBuilder;
        /// <summary>
        /// Common Command builder.
        /// </summary>
        public DataSetCommonCommandBuilder CommonCommandBuilder
        {
            get
            {
                return this.commonCommandBuilder;
            }
        }
        #endregion
        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="type">Type of data object.</param>
        public DataSetCommandBuilder(System.Data.IDbConnection connection, System.Type type)
        {
            this.connection = connection;
            this.commonCommandBuilder = new DataSetCommonCommandBuilder(type);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="type">Type of data object.</param>
        /// <param name="name">Data object name.</param>
        public DataSetCommandBuilder(System.Data.IDbConnection connection, System.Type type, string name)
        {
            this.connection = connection;
            this.commonCommandBuilder = new DataSetCommonCommandBuilder(type, name);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="commonCommandBuilder">dataset Common Command builder.</param>
        public DataSetCommandBuilder(System.Data.IDbConnection connection, DataSetCommonCommandBuilder commonCommandBuilder)
        {
            this.connection = connection;
            this.commonCommandBuilder = commonCommandBuilder;
        }
   
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="type">Type of data object.</param>
        public DataSetCommandBuilder(System.Data.IDbConnection connection, DatabaseType databaseType, string providerName, System.Type type)
        {
            this.connection = connection;
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.commonCommandBuilder = new DataSetCommonCommandBuilder(type);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="type">Type of data object.</param>
        /// <param name="name">Data object name.</param>
        public DataSetCommandBuilder(System.Data.IDbConnection connection, DatabaseType databaseType, string providerName, System.Type type, string name)
        {
            this.connection = connection;
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.commonCommandBuilder = new DataSetCommonCommandBuilder(type, name);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connection">database connection.</param>
        /// <param name="commonCommandBuilder">dataset Common Command builder.</param>
        public DataSetCommandBuilder(System.Data.IDbConnection connection, DatabaseType databaseType, string providerName, DataSetCommonCommandBuilder commonCommandBuilder)
        {
            this.connection = connection;
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.commonCommandBuilder = commonCommandBuilder;
        }
        #endregion

        /// <summary>
        /// Gets Command builder.
        /// </summary>
        /// <param name="name">table name.</param>
        /// <returns>Command builder.</returns>
        public CommandBuilder GetCommandBuilder(string name)
        {
            DatabaseType databaseType = GetDatabaseType();
            return new CommandBuilder(this.connection, databaseType, this._providerName, this.commonCommandBuilder.GetCommonCommandBuilder(name));
        }

        
        /// <summary>
        /// Create Command.
        /// </summary>
        /// <returns>Command.</returns>
        protected virtual System.Data.IDbCommand CreateCommand()
        {
            return this.connection.CreateCommand();
        }

        /// <summary>
        /// Gets the Mapping coommand.
        /// </summary>
        /// <param name="commandCommand">Common Command.</param>
        /// <returns>the Mapping coommand.</returns>
        public virtual System.Data.IDbCommand GetMappingCommand(RaisingStudio.Data.CommonCommand commandCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetMappingCommand(commandCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }


        /// <summary>
        /// Gets Relation Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="tableName">table name.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>Relation Command.</returns>
        public virtual System.Data.IDbCommand GetRelationCommand(DataRelationDefinition dataRelationDefinition, DataTableDefinition dataTableDefinition, CommonCommand conditionCommand, bool mappingCommand, DataTableDefinition parentTableDefinition, string parentTableName, DataTableDefinition childTableDefinition, string childTableName)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetRelationCommand(dataRelationDefinition, dataTableDefinition, conditionCommand, mappingCommand, parentTableDefinition, parentTableName, childTableDefinition, childTableName);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }


        /// <summary>
        /// Gets parent Relation Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation Command.</returns>
        public virtual System.Data.IDbCommand GetParentRelationCommand(DataRelationDefinition dataRelationDefinition, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentRelationCommand(dataRelationDefinition, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets parent Relation Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation Command.</returns>
        public virtual System.Data.IDbCommand GetParentRelationCommand(string relationName, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentRelationCommand(relationName, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation parent query Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation parent query Command.</returns>
        public virtual System.Data.IDbCommand GetParentCommand(string relationName, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentCommand(relationName, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }
        
        /// <summary>
        /// Gets Relation parent query Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation parent query Command.</returns>
        public virtual System.Data.IDbCommand GetParentCommand(string relationName, string[] columnNames, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentCommand(relationName, columnNames, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation parent query Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation parent query Command.</returns>
        public virtual System.Data.IDbCommand GetParentCommand(DataRelationDefinition dataRelationDefinition, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentCommand(dataRelationDefinition, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation parent query Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation parent query Command.</returns>
        public virtual System.Data.IDbCommand GetParentCommand(DataRelationDefinition dataRelationDefinition, string[] columnNames, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentCommand(dataRelationDefinition, columnNames, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation parent query Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation parent query Command.</returns>
        public virtual System.Data.IDbCommand GetParentSelectCommand(string relationName, object[] primaryKeys)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentSelectCommand(relationName, primaryKeys);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }
        
        /// <summary>
        /// Gets Relation parent query Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation parent query Command.</returns>
        public virtual System.Data.IDbCommand GetParentSelectCommand(string relationName, string[] columnNames, object[] primaryKeys)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentSelectCommand(relationName, columnNames, primaryKeys);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation parent query Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation parent query Command.</returns>
        public virtual System.Data.IDbCommand GetParentSelectCommand(DataRelationDefinition dataRelationDefinition, object[] primaryKeys)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentSelectCommand(dataRelationDefinition, primaryKeys);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation parent query Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation parent query Command.</returns>
        public virtual System.Data.IDbCommand GetParentSelectCommand(DataRelationDefinition dataRelationDefinition, string[] columnNames, object[] primaryKeys)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentSelectCommand(dataRelationDefinition, columnNames, primaryKeys);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation parent query Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>Relation parent query Command.</returns>
        public virtual System.Data.IDbCommand GetParentSelectCommand(string relationName, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentSelectCommand(relationName, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }
        
        /// <summary>
        /// Gets Relation parent query Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>Relation parent query Command.</returns>
        public virtual System.Data.IDbCommand GetParentSelectCommand(string relationName, string[] columnNames, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentSelectCommand(relationName, columnNames, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation parent query Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>Relation parent query Command.</returns>
        public virtual System.Data.IDbCommand GetParentSelectCommand(DataRelationDefinition dataRelationDefinition, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentSelectCommand(dataRelationDefinition, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation parent query Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>Relation parent query Command.</returns>
        public virtual System.Data.IDbCommand GetParentSelectCommand(DataRelationDefinition dataRelationDefinition, string[] columnNames, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetParentSelectCommand(dataRelationDefinition, columnNames, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }


        /// <summary>
        /// Gets child Relation Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation Command.</returns>
        public virtual System.Data.IDbCommand GetChildRelationCommand(DataRelationDefinition dataRelationDefinition, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildRelationCommand(dataRelationDefinition, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets child Relation Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation Command.</returns>
        public virtual System.Data.IDbCommand GetChildRelationCommand(string relationName, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildRelationCommand(relationName, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child query Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation child query Command.</returns>
        public virtual System.Data.IDbCommand GetChildCommand(string relationName, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildCommand(relationName, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child query Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation child query Command.</returns>
        public virtual System.Data.IDbCommand GetChildCommand(string relationName, string[] columnNames, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildCommand(relationName, columnNames, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child query Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation child query Command.</returns>
        public virtual System.Data.IDbCommand GetChildCommand(DataRelationDefinition dataRelationDefinition, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildCommand(dataRelationDefinition, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child query Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation child query Command.</returns>
        public virtual System.Data.IDbCommand GetChildCommand(DataRelationDefinition dataRelationDefinition, string[] columnNames, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildCommand(dataRelationDefinition, columnNames, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child query Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation child query Command.</returns>
        public virtual System.Data.IDbCommand GetChildSelectCommand(string relationName, object[] primaryKeys)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildSelectCommand(relationName, primaryKeys);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child query Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation child query Command.</returns>
        public virtual System.Data.IDbCommand GetChildSelectCommand(string relationName, string[] columnNames, object[] primaryKeys)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildSelectCommand(relationName, columnNames, primaryKeys);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child query Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation child query Command.</returns>
        public virtual System.Data.IDbCommand GetChildSelectCommand(DataRelationDefinition dataRelationDefinition, object[] primaryKeys)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildSelectCommand(dataRelationDefinition, primaryKeys);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child query Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation child query Command.</returns>
        public virtual System.Data.IDbCommand GetChildSelectCommand(DataRelationDefinition dataRelationDefinition, string[] columnNames, object[] primaryKeys)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildSelectCommand(dataRelationDefinition, columnNames, primaryKeys);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child query Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>Relation child query Command.</returns>
        public virtual System.Data.IDbCommand GetChildSelectCommand(string relationName, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildSelectCommand(relationName, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child query Command.
        /// </summary>
        /// <param name="relationName">relation name.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>Relation child query Command.</returns>
        public virtual System.Data.IDbCommand GetChildSelectCommand(string relationName, string[] columnNames, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildSelectCommand(relationName, columnNames, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child query Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>Relation child query Command.</returns>
        public virtual System.Data.IDbCommand GetChildSelectCommand(DataRelationDefinition dataRelationDefinition, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildSelectCommand(dataRelationDefinition, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child query Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>Relation child query Command.</returns>
        public virtual System.Data.IDbCommand GetChildSelectCommand(DataRelationDefinition dataRelationDefinition, string[] columnNames, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildSelectCommand(dataRelationDefinition, columnNames, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }
        

        /// <summary>
        /// Gets Relation child Delete Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation child Delete Command.</returns>
        public virtual System.Data.IDbCommand GetChildDeleteCommand(DataRelationDefinition dataRelationDefinition, object[] primaryKeys)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildDeleteCommand(dataRelationDefinition, primaryKeys);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child Delete Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>Relation child Delete Command.</returns>
        public virtual System.Data.IDbCommand GetChildDeleteCommand(DataRelationDefinition dataRelationDefinition, CommonCommand conditionCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetChildDeleteCommand(dataRelationDefinition, conditionCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets Relation child Delete Command.
        /// </summary>
        /// <param name="dataRelationDefinition">Definition of relation.</param>
        /// <param name="parentCondition">parent Condition 。</param>
        /// <param name="childCondition">child Condition 。</param>
        /// <returns>Relation child Delete Command.</returns>
        public virtual System.Data.IDbCommand GetDeleteChildCommand(DataRelationDefinition dataRelationDefinition, CommonCommand parentCondition, CommonCommand childCondition, bool mappingCommand)
        {
            CommonCommand commonCommand = this.commonCommandBuilder.GetDeleteChildCommand(dataRelationDefinition, parentCondition, childCondition, mappingCommand);
            System.Data.IDbCommand command = ConvertCommand(commonCommand);
            return command;
        }

        /// <summary>
        /// Gets table Delete Command.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <returns>table Delete Command.</returns>
        public virtual System.Data.IDbCommand GetDeleteCommand(string tableName)
        {
            CommandBuilder commandBuilder = this.GetCommandBuilder(tableName);
            return commandBuilder.GetDeleteCommand();
        }

        /// <summary>
        /// Gets table Delete Command.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <returns>table Delete Command.</returns>
        public virtual System.Data.IDbCommand GetDeleteCommand(string tableName, object[] primaryKeys)
        {
            CommandBuilder commandBuilder = this.GetCommandBuilder(tableName);
            System.Data.Common.DataTableMapping dataTableMapping = commandBuilder.CommonCommandBuilder.GetDataTableMapping();
            System.Data.IDbCommand command = commandBuilder.GetDeleteCommand(dataTableMapping, false);
            for (int i = 0; i < command.Parameters.Count; i++)
            {
                (command.Parameters[i] as System.Data.IDbDataParameter).Value = primaryKeys[i];
            }
            return command;
        }

        /// <summary>
        /// Gets table Delete Command.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>table Delete Command.</returns>
        public virtual System.Data.IDbCommand GetDeleteCommand(string tableName, CommonCommand conditionCommand)
        {
            CommandBuilder commandBuilder = this.GetCommandBuilder(tableName);
            return commandBuilder.GetDeleteCommand(conditionCommand);
        }


        public virtual DatabaseType GetDatabaseType()
        {
            if ((this._databaseType == DatabaseType.OTHER) && (!ProviderManager.Instance.ProviderExists(this._providerName)))
            {
                DatabaseType databaseType = ConnectionFactory.GetDatabaseType(this.connection);
                return databaseType;
            }
            return this._databaseType;
        }

        protected virtual System.Data.IDbCommand ConvertCommand(CommonCommand commonCommand)
        {
            DatabaseType databaseType = GetDatabaseType();
            return CommandConverter.Convert(commonCommand, this.connection, databaseType, this._providerName);
        }
    }
}
